/*
 * HttpApi.java
 * classes : com.ruint.master.api.HttpApi
 * @author ruint
 * V 1.0.0
 * Create at 2014-11-27 上午11:05:32
 */
package com.ruint.master.api;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.ruint.core.utils.volley.BaseHttpApi;
import com.ruint.core.utils.volley.RequestManager;
import com.ruint.master.app.constant.AppConstant;
import com.ruint.master.bean.Diary;
import com.ruint.master.bean.DiaryWrapper;
import com.ruint.master.bean.HasHeadResult;
import com.ruint.master.bean.ImageItem;
import com.ruint.master.bean.NetImgUrlWrapper;
import com.ruint.master.bean.User;

/**
 * com.ruint.master.api.HttpApi
 * 
 * @author ruint <br/>
 *         create at 2014-11-27 上午11:05:32
 */
public class HttpApi extends BaseHttpApi implements AppConstant.ApiConstant {

  @Override
  protected String getHost() {
    return BASE_URL;
  }

  @Override
  protected String getService() {
    return BASE_SERVICE;
  }

  @Override
  protected void addCommonParams(List<NameValuePair> pList) {
  }

  @Override
  protected void addCommonParams(Map<String, String> params) {
  }

  public void login(Context context, String username, String password,
      RequestManager.GenericCallback<HasHeadResult<User>> callback) {
    List<NameValuePair> pList = new ArrayList<NameValuePair>();
    pList.add(new BasicNameValuePair("username", username));
    pList.add(new BasicNameValuePair("password", password));
    addCommonParams(pList);
    post(context, new HasHeadResult<User>(), ACTION_LOGIN, pList, callback);
  }

  public void getDiary(Context context, String page, String pagesize, String isself,
      RequestManager.GenericCallback<HasHeadResult<List<Diary>>> callback) {
    List<NameValuePair> pList = new ArrayList<NameValuePair>();
    pList.add(new BasicNameValuePair("page", page));
    pList.add(new BasicNameValuePair("pagesize", pagesize));
    pList.add(new BasicNameValuePair("isself", isself));
    addCommonParams(pList);
    get(context, new HasHeadResult<List<Diary>>(), ACTION_DIARY_LIST, pList, callback);
  }

  public void getDiaryList(Context context, RequestManager.GenericCallback<DiaryWrapper> callback) {
    List<NameValuePair> pList = new ArrayList<NameValuePair>();
    pList.add(new BasicNameValuePair("page", 1 + ""));
    pList.add(new BasicNameValuePair("pagesize", 20 + ""));
    pList.add(new BasicNameValuePair("isself", 0 + ""));
    addCommonParams(pList);
    get(context, new DiaryWrapper(), ACTION_DIARY_LIST, pList, callback);
  }

  public void postImage(Context context, List<ImageItem> imgs, RequestManager.GenericCallback<NetImgUrlWrapper> callback) {
    List<NameValuePair> pList = new ArrayList<NameValuePair>();
    addCommonParams(pList);
    Map<String, File> files = new HashMap<String, File>();
    for (int i = 0; i < imgs.size(); i++) {
      files.put(i + "", new File(imgs.get(i).getPath()));
    }
    post(context, new NetImgUrlWrapper(), ACTION_UPLOAD_IMAGE, pList, files, true, callback);
  }

}